package com.creative.core.uitls;

import java.security.SecureRandom;
import java.util.Random;

/**
 * 随机生成id
 * 创建人: ling
 * 创建时间: 2024/11/20
 */
public class Uidutil {

    /**
     * 生成随机长度的id
     *  范围：0-length
     * @param length 长度
     * @return {@link String }
     * @author ling
     * @date 2024/11/20
     */
    public static String generateUid(int length) {

//        if (length < 1 || length > 10) {
//            throw new IllegalArgumentException("Length must be between 1 and 10");
//        }
        Random randomnum = new Random();
//        随机生成1-10间的自然数   就是随机生成多少位的id  id长度
        int num = randomnum.nextInt(length);

        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(num);
        for (int i = 0; i < num; i++) {
            char c = (char) ('0' + random.nextInt(10)); // 生成0-9之间的数字
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 生成会话id 5位以内
     *
     * @return {@link String }
     * @author ling
     * @date 2024/11/26
     */
    public static  String generateSessionId() {
        Random randomnum = new Random();
//        随机生成1-5间的自然数   就是随机生成多少位的id  id长度
        int num = randomnum.nextInt(5);
        SecureRandom random = new SecureRandom();

        StringBuilder sb = new StringBuilder(num);
        for (int i = 0; i < num; i++) {
            char c = (char) ('0' + random.nextInt(10)); // 生成0-9之间的数字
            sb.append(c);
        }
        return sb.toString();
    }

}
